#include "myformdoc.h"

#include "qfile.h"
#include "qtextstream.h"
#include "qfileinfo.h"
#include "qfont.h"
#include "qfontdialog.h"

MyFormDoc::MyFormDoc(QWidget *parent)
	: QWidget(parent)
	, ui(new Ui::MyFormDocClass())
{
	ui->setupUi(this);
	setWindowTitle("New Doc[*]");
	setAttribute(Qt::WA_DeleteOnClose);

	//有变化，设置窗口变化（右上角显示*）
	connect(ui->plainTextEdit, &QPlainTextEdit::modificationChanged,
		this, &MyFormDoc::setWindowModified);
}

MyFormDoc::~MyFormDoc()
{
	delete ui;
}

void MyFormDoc::load_from_file(QString& path)
{
	QFile a_file(path);
	if (a_file.open(QIODevice::ReadOnly | QIODevice::Text))
	{
		QTextStream a_stream(&a_file);
		ui->plainTextEdit->clear();
		ui->plainTextEdit->setPlainText(a_stream.readAll());
		a_file.close();

		QFileInfo a_file_info(path);
		QString str = a_file_info.fileName();
		setWindowTitle(str + "[*]");
		file_name_now = str;
		file_opened = true;
	}
}

QString MyFormDoc::current_file_name()
{
	return file_name_now;
}

bool MyFormDoc::is_file_opened()
{
	return file_opened;
}

void MyFormDoc::save_to_file()
{
	//这里我们不真的去保存，只弄个样子
	setWindowModified(false);//设置窗口为没有变化（右上角的*）
}

void MyFormDoc::set_edit_font()
{
	QFont font;
	font = ui->plainTextEdit->font();
	bool result;
	font = QFontDialog::getFont(&result, font);
	if (result)
		ui->plainTextEdit->setFont(font);
}

void MyFormDoc::text_cut()
{
	ui->plainTextEdit->cut();
}

void MyFormDoc::text_copy()
{
	ui->plainTextEdit->copy();
}

void MyFormDoc::text_paste()
{
	ui->plainTextEdit->paste();
}
